import { login, logout, getInfo, getRouter } from '@/api/login'
import { setStore, getStore, removeStore } from '@/utils/store'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getHandleList } from '@/api/common'
import store from '@/store'
const user = {
  state: {
    token: getToken(),
    routers: [],
    userInfo: {},
    isLock: getStore({ name: 'isLock' }) || false,
    lockPasswd: getStore({ name: 'lockPasswd' }) || '',
    jurisdiction: getStore({ name: 'jurisdiction' }) || '',
    browserHeaderTitle: getStore({ name: 'browserHeaderTitle' }) || '后台管理系统'
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_ROUTER: (state, routers) => {
      state.routers = routers
    },
    SET_USERINFO: (state, userInfo) => {
      state.userInfo = userInfo
    },
    SET_LOCK_PASSWD: (state, lockPasswd) => {
      state.lockPasswd = lockPasswd
      setStore({
        name: 'lockPasswd',
        content: state.lockPasswd,
        type: 'session'
      })
    },
    SET_LOCK: (state, action) => {
      state.isLock = true
      setStore({
        name: 'isLock',
        content: state.isLock,
        type: 'session'
      })
    },
    CLEAR_LOCK: (state, action) => {
      state.isLock = false
      state.lockPasswd = ''
      state.jurisdiction = ''
      removeStore({
        name: 'jurisdiction'
      })
      removeStore({
        name: 'lockPasswd'
      })
      removeStore({
        name: 'isLock'
      })
    },
    SET_BROWSERHEADERTITLE: (state, action) => {
      state.browserHeaderTitle = action.browserHeaderTitle
    },
    SET_JUR: (state, jurisdiction) => {
      state.jurisdiction = jurisdiction
      setStore({
        name: 'jurisdiction',
        content: state.jurisdiction,
        type: 'session'
      })
    },
    CLEAR_JUR: (state, action) => {
      state.jurisdiction = ''
      removeStore({
        name: 'jurisdiction'
      })
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      console.log(userInfo)
      return new Promise((resolve, reject) => {
        login(username, userInfo.password, userInfo.type).then(response => {
          const data = response
          if (data.data){
          	setToken(data.data.token)
          	commit('SET_TOKEN', data.data.token)
          } 
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
		// 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(response => {
        	commit('SET_USERINFO', response.data)
        	store.userInfo = response.data
        	//this.$store.userInfo=res.data
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    // 获取路由信息
    GetRouter({ commit, state }) {
      return new Promise((resolve, reject) => {
        getRouter().then(response => {
          const data = response
          store.AllMenu = data.data
          if (data.data && data.data.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROUTER', data.data)
          } else {
            reject('getRouter: router must be a non-null array !')
          }
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },

    
    // 登出
    LogOut({ commit, state }) {
      return new Promise((resolve, reject) => {
        logout().then(() => {
          commit('CLEAR_LOCK')
          commit('SET_TOKEN', '')
          removeToken()
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    },

    // 角色权限获取
    Juage({ commit }, handleId) {
      return new Promise((resolve, reject) => {
        const data = store.AllMenu
        var jurId = store.getters.jurisdiction
        if (data.length > 0) {
          if (!handleId) {
            handleId = jurId
          }
          for (let i = 0; i < data.length; i++) {
            if (data[i].children.length) {
              for (let j = 0; j < data[i].children.length; j++) {
                if (data[i].children[j].id === handleId) {
                	const para = {
			                    handleType: 'page',
			                    userId:store.userInfo.id,
			                    pid: data[i].children[j].id
			                  }
			                  getHandleList(para).then(res => {
			                    resolve(res)
			                  }).catch(error => {
			                    reject(error)
			                  })
                  break
                }
              }
            }
          }
        }
      })
    }
  }
}

export default user
